package com.pkk.alert.web.controller.feige;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pkk.alert.common.dto.FeiGeReqDataVo;
import com.pkk.alert.common.feige.FeigeComonBuild;
import com.pkk.alert.common.request.feige.FeigeUserPageReqData;
import com.pkk.alert.entity.feige.FeigeAccount;
import com.pkk.alert.entity.feige.FeigeTemplate;
import com.pkk.alert.service.FeigeAccountService;
import com.pkk.alert.service.FeigeTemplateService;
import com.pkk.alert.service.impl.FeigeUserEntityServiceImpl;
import com.pkk.components.rpc.request.PagedQueryRequest;
import com.pkk.components.rpc.response.CommonResponse;
import com.pkk.components.rpc.response.util.ResponseUtil;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Optional;
import javax.annotation.Resource;
import javax.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: 飞鸽的控制器
 * @author: peikunkun
 * @create: 2019-07-02 15:37
 **/
@Slf4j
@RestController
@RequestMapping("/feige")
//@Api(tags = "飞鸽控制器")
public class FeiGeController {

  @Resource
  private FeigeUserEntityServiceImpl feigeUserEntityService;
  @Resource
  private FeigeAccountService feigeAccountService;
  @Resource
  private FeigeTemplateService feigeTemplateService;

  ////@BusinessLog(desc = "报警测试")
  @RequestMapping(value = "/alarm")
  //@ApiOperation(value = "报警测试", httpMethod = "POST")
  public CommonResponse alarm() {
    FeigeAccount useFeigeAccount = feigeAccountService.getUseFeigeAccount();
    feigeAccountService.successFeigeAccount(useFeigeAccount.getId(), feigeUserEntityService.countUser(useFeigeAccount.getId()));
    return ResponseUtil.success(useFeigeAccount);
  }


  /**
   * @Description: 发送单用户信息
   * @Param: []
   * @return: com.pkk.components.util.response.CommonResponse<java.lang.Boolean>
   * @Author: peikunkun
   * @Date: 2019/7/5 0005 下午 4:58
   */
  ////@BusinessLog(desc = "发送单用户信息")
  @RequestMapping(value = "/sendFeigeSingleUser")
  //@ApiOperation(value = "发送单用户信息", httpMethod = "POST")
  public CommonResponse<Boolean> sendFeigeSingleUser(String templateId, String uid,
      HashMap<String, FeiGeReqDataVo> param) {
    FeigeAccount useFeigeAccount = feigeAccountService.getUseFeigeAccount();
    templateId = Optional.ofNullable(templateId).orElse("5uZIvSm5GAdUR1X25HNpjuOp6jSiL88v4opn4a4GLa0");
    String skipUrl = "http://t.cn/AiO6XSvR";
    QueryWrapper<FeigeTemplate> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("template_id", templateId);
    FeigeTemplate feigeTemplate = feigeTemplateService.getOne(queryWrapper);
    if (null == feigeTemplate) {
      return ResponseUtil.error("不知道的模板信息");
    }
    if (param == null || param.size() <= 0) {
      param.put("first", FeiGeReqDataVo.builder().value("My queen").build());
      param.put("keyword1", FeiGeReqDataVo.builder().value("国歌,我爱你").build());
      param.put("keyword2", FeiGeReqDataVo.builder().value("你的臭虫").build());
      param.put("keyword3",
          FeiGeReqDataVo.builder().value(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))
              .build());
      param.put("remark", FeiGeReqDataVo.builder().value("I Love You").build());
//      return ResponseUtil.error("模板内容不能为空");
    }
    boolean sendResult = FeigeComonBuild
        .buildSendSingleUser(useFeigeAccount.getSecret(), Integer.valueOf(uid), templateId,
            feigeTemplate.getTemplateContent(), skipUrl, param);
    if (sendResult) {
      log.info("发送微信通知成功");
      feigeAccountService.successFeigeAccount(useFeigeAccount.getId(), feigeUserEntityService.countUser(useFeigeAccount.getId()));
    }
    return ResponseUtil.success();
  }


  /**
   * @Description: 更新内页域名信息接口
   * @Param: [request]
   * @return:CommonResponse<Boolean>
   * @Author: peikunkun
   * @Date: 2019/6/28 0021 上午 09:39
   */
  //@BusinessLog(desc = "同步飞鸽的所有用户信息")
  @RequestMapping(value = "/syncFeigeUser")
  //@ApiOperation(value = "同步飞鸽的所有用户信息", httpMethod = "POST")
  public CommonResponse syncFeigeUser() {
    feigeUserEntityService.syncFeigeUser();
    return ResponseUtil.success(true);
  }


  /**
   * @Description: 查询飞鸽用户信息-分页
   * @Param: []
   * @return: com.pkk.components.util.response.CommonResponse
   * @Author: peikunkun
   * @Date: 2019/7/3 0003 上午 11:49
   */
  //@BusinessLog(desc = "查询飞鸽用户信息-分页")
  @RequestMapping("findFeigeUserPage")
  //@ApiOperation(value = "查询飞鸽用户信息", httpMethod = "POST")
  public CommonResponse findFeigeUserPage(@RequestBody @Valid PagedQueryRequest<FeigeUserPageReqData> request) {
    return ResponseUtil.success(feigeUserEntityService.findFeigeUserPage(request));
  }

}
